package com.ruoyi.flowable.listener.repair;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquRepair;
import com.ruoyi.equ.service.IEquRepairService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 维修-4监听
 */
@Slf4j
@Component("repairListener_4")
public class RepairListener_4 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final IEquRepairService repairService = SpringUtils.getBean(IEquRepairService.class);

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //4.维修自检结果
            HashMap<String, String> content4 = new HashMap<>();
            content4.put("设备运行", execution.getVariable("flag4_1", String.class));
            content4.put("维修部位", execution.getVariable("flag4_2", String.class));
            content4.put("现场", execution.getVariable("flag4_3", String.class));
            //赋值
            HashMap<String, Object> repairCheckMap = new HashMap<>();
            repairCheckMap.put("维修自检结果", content4);
            String repairCheck = objectMapper.writeValueAsString(repairCheckMap);

            //updateBy
            String updateBy = SecurityUtils.getLoginUser().getUser().getNickName();

            EquRepair equRepair = execution.getVariable("equRepair", EquRepair.class);
            equRepair.setRepairCheck(repairCheck);
            equRepair.setUpdateBy(updateBy);

            //修改
            repairService.updateEquRepair(equRepair);
            //json
            execution.setVariable("equRepair", equRepair);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
